package test;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.thoughtworks.xstream.annotations.*;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@XStreamAlias("person")
public class Person {
    public Person() {
        this.customListeners = new HashSet<>();
    }

    @XStreamAlias("personName")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private boolean adult;

    public boolean isAdult() {
        return adult;
    }

    public void setAdult(boolean input) {
        adult = input;
    }

    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAgeStr() {
        return String.valueOf(age);
    }

    public void setAgeStr(String input) {
        age = Integer.valueOf(input);
    }

    @JsonProperty(value = "UPPER_PROP")
    private String UPPER_PROP;

    @JSONField(name = "UPPER_PROP")
    public String getUPPER_PROP() {
        return UPPER_PROP;
    }

    @JSONField(name = "UPPER_PROP")
    public void setUPPER_PROP(String UPPER_PROP) {
        this.UPPER_PROP = UPPER_PROP;
    }

    @XStreamOmitField
    private School school;

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    private Set<CustomListener> customListeners;

    /**
     * 添加监听器
     * @param customListener
     */
    public synchronized void addCustomListener(CustomListener customListener) {
        this.customListeners.add(customListener);
    }
    /**
     * 移除监听器
     * @param customListener
     */
    public synchronized void removeCustomListener(CustomListener customListener) {
        this.customListeners.remove(customListener);
    }

    //以下的监听器不会被内省机制发现，监听器的名称须与参数的类型名称一致
    public void addMyListener(CustomListener customListener) {
        this.customListeners.add(customListener);
    }

    public void removeMyListener(CustomListener customListener) {
        this.customListeners.remove(customListener);
    }

    public void fireEvent() {
		this.customListeners.forEach(m->m.customPerformed(new CustomEvent(this,"事件测试")));
    }
}


